package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"recruit-ease/cache"
	"recruit-ease/models"
	"time"
)

func List(c *gin.Context) {

	var work []models.Work

	redis_Key := "work_list"
	errData, _ := cache.Rdb.Get(cache.Ctx, redis_Key).Result()
	if errData == "" {
		models.DB.Find(&work)
		jsonData, _ := json.Marshal(work)
		cache.Rdb.Set(cache.Ctx, redis_Key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(errData), &work)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"work": work,
		},
	})
	return
}

func Detail(c *gin.Context) {
	id := c.Query("id")
	var work models.Work
	models.DB.Where("id = ?", id).First(&work)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询详情成功",
		"data": work,
	})
	return
}

func Collection(c *gin.Context) {

}
